﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CYTS.Aoyou.Framework.Utility
{
    /// <summary>
    /// Cookie帮助类
    /// </summary>
    public class CookieHelper
    {
        /// <summary>
        /// 写入Cookies
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="subKeyName">cookie键名</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="expireTime">日期</param>
        /// <returns>是否成功</returns>
        public static bool SetCookie(string cookieName, string subKeyName, string cookieValue, DateTime expireTime)
        {
            if (string.IsNullOrEmpty(cookieName) || string.IsNullOrEmpty(subKeyName))
            {
                throw new Exception("cookie名称或者cookie键名不能为空");
            }
   
            HttpCookie objCookie;
            //先判断是否有Cookies对象。如果有则需要先获取。
            if (System.Web.HttpContext.Current.Response.Cookies[cookieName] == null)
            {
                objCookie = new HttpCookie(cookieName);
            }
            else
            {
                objCookie = System.Web.HttpContext.Current.Response.Cookies[cookieName];
            }


            //设定cookie 过期时间.
            objCookie.Expires = expireTime;

            //设定cookie 域名.
            string domain = string.IsNullOrEmpty(HttpContext.Current.Request.Params["HTTP_HOST"]) ? "/" : HttpContext.Current.Request.Params["HTTP_HOST"];

            if (domain.IndexOf(".") > -1)
            {
                string[] temp = domain.Split('.');

                if (temp.Length >= 3)
                {
                    domain = temp[temp.Length - 2].Trim() + "." + temp[temp.Length - 1].Trim();
                }
                objCookie.Domain = domain;
            }

            //写入Cookies   
            objCookie[subKeyName] = cookieValue;

            if (System.Web.HttpContext.Current.Response.Cookies[cookieName] == null)
            {
                System.Web.HttpContext.Current.Response.Cookies.Add(objCookie);
            }
            else
            {
                System.Web.HttpContext.Current.Response.Cookies.Set(objCookie);
            }

            return true;
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="cookieName">Cookie键名</param>
        /// <returns>Cookies值</returns>
        public static string GetCookie(string cookieName)
        {
            return GetCookie(cookieName, null);
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="cookieName">Cookie键名</param>
        /// <param name="keyName">子键名</param>
        /// <returns>Cookies值</returns>
        public static string GetCookie(string cookieName, string keyName)
        {
            string result = string.Empty;

            try
            {
                if (HttpContext.Current.Request.Cookies[cookieName] != null && !String.IsNullOrEmpty(keyName))
                {
                    result = HttpContext.Current.Request.Cookies[cookieName][keyName];
                }
            }
            catch
            {
                result = string.Empty;
            }

            return result;
        }

    }
}
